# Load packages
librarian::shelf(
    dplyr, readr, skimr, tidyr,
    quiet = TRUE
)

# Overwrite select function
select <- dplyr::select

# Import data
data <- read_csv(
    "Data/Meta/braleyEtAl_2023/pilot_data.csv",
    show_col_types = FALSE
)

# Remove junk rows
data <- data[-c(1, 2), ]

# Remove people who didn't consent to the survey
data <- data %>%
    subset(consent == "Yes")

# Remove people who failed comprehension checks
data <- data %>% subset(
    t_meta_rep_attention == "Probably Not" | is.na(t_meta_rep_attention)
)

data <- data %>% subset(
    t_meta_dem_attention == "Probably Not" | is.na(t_meta_dem_attention)
)

data <- data %>% subset(
    c_meta_rep_attention == "Probably Not" | is.na(c_meta_rep_attention)
)

data <- data %>% subset(
    c_meta_dem_attention == "Probably Not" | is.na(c_meta_dem_attention)
)

# Remove people who were kicked out for being Independents
data <- data %>% subset(t_ind_leaning != "Neither" | is.na(t_ind_leaning))
data <- data %>% subset(c_ind_leaning != "Neither" | is.na(c_ind_leaning))

# Rename columns
data <- data %>%
    rename(
        condition = score
    )

# Drop missing values
data[data == "-99"] <- NA_character_

#################
### CONDITION ###
#################

# Recode treatment column
data$condition <- ifelse(is.na(data$condition), "control", "treatment")

######################
### DEHUMANIZATION ###
######################

# Consolidate answers
data$dehum_t <- ifelse(is.na(data$t_rep_dehum_1), data$t_dem_dehum_1, data$t_rep_dehum_1)
data$dehum_c <- ifelse(is.na(data$c_rep_dehum_1), data$c_dem_dehum_1, data$c_rep_dehum_1)
data$dehumanization <- ifelse(is.na(data$dehum_t), data$dehum_c, data$dehum_t)

# Make numeric and flip
data$dehumanization <- 100 - data$dehumanization %>% as.numeric()

##################
### SUBVERSION ###
##################

# Consolidate answers
data$rallies_t <- ifelse(is.na(data$t_rep_rallies), data$t_dem_rallies, data$t_rep_rallies)
data$rallies_c <- ifelse(is.na(data$c_rep_rallies), data$c_dem_rallies, data$c_rep_rallies)
data$rallies <- ifelse(is.na(data$rallies_t), data$rallies_c, data$rallies_t)

data$judges_t <- ifelse(is.na(data$t_rep_judges), data$t_dem_judges, data$t_rep_judges)
data$judges_c <- ifelse(is.na(data$c_rep_judges), data$c_dem_judges, data$c_rep_judges)
data$judges <- ifelse(is.na(data$judges_t), data$judges_c, data$judges_t)

data$journ_t <- ifelse(is.na(data$t_rep_journ), data$t_dem_journ, data$t_rep_journ)
data$journ_c <- ifelse(is.na(data$c_rep_journ), data$c_dem_journ, data$c_rep_journ)
data$journ <- ifelse(is.na(data$journ_t), data$journ_c, data$journ_t)

data$voting_t <- ifelse(is.na(data$t_rep_voting), data$t_dem_voting, data$t_rep_voting)
data$voting_c <- ifelse(is.na(data$c_rep_voting), data$c_dem_voting, data$c_rep_voting)
data$voting <- ifelse(is.na(data$voting_t), data$voting_c, data$voting_t)

data$elected_t <- ifelse(is.na(data$t_rep_elected), data$t_dem_elected, data$t_rep_elected)
data$elected_c <- ifelse(is.na(data$c_rep_elected), data$c_dem_elected, data$c_rep_elected)
data$elected <- ifelse(is.na(data$elected_t), data$elected_c, data$elected_t)

data$violence_t <- ifelse(is.na(data$t_rep_violence), data$t_dem_violence, data$t_rep_violence)
data$violence_c <- ifelse(is.na(data$c_rep_violence), data$c_dem_violence, data$c_rep_violence)
data$violence <- ifelse(is.na(data$violence_t), data$violence_c, data$violence_t)

data$const_t <- ifelse(is.na(data$t_rep_const), data$t_dem_const, data$t_rep_const)
data$const_c <- ifelse(is.na(data$c_rep_const), data$c_dem_const, data$c_rep_const)
data$const <- ifelse(is.na(data$const_t), data$const_c, data$const_t)

# Recode Likert responses
data <- data %>%
    mutate(
        across(
            c(rallies, judges, journ, voting, elected, violence, const),
            ~ case_when(
                grepl("Never", .) ~ 0,
                grepl("Probably Not", .) ~ 1,
                grepl("Probably", .) ~ 2,
                grepl("Definitely", .) ~ 3
            )
        )
    )

# Generate index
data$subversion <- data %>%
    select(rallies, judges, journ, voting, elected, violence, const) %>%
    rowMeans(na.rm = TRUE)

# Get ranges
max(data$subversion) - min(data$subversion)
max(data$dehumanization) - min(data$dehumanization)

##############
### MODELS ###
##############

# Democratic Subversion
lm(subversion ~ condition, data = data) %>% summary()

# Dehumanization
lm(dehumanization ~ condition, data = data) %>% summary()
